import 'package:flutter_slidable_panel/controllers/slide_controller.dart';
import 'package:get/get.dart';
import 'package:wk_robot2/app/robot_comm/mqtt_comm_manager.dart';

import '../../../const/AppConst.dart';
import '../../../event/DataCallBack.dart';
import '../../../event/MsgContentEvent.dart';
import '../../../logger/Logger.dart';
import '../../../models/Devices.dart';
import '../../../nets/NetWorkManager.dart';
import '../../../storage/StorageManager.dart';
import '../../../utils/CommonUtil.dart';
import '../../../widget/tips.dart';
import '../../componentview/controllers/IndexViewController.dart';

class MineRobotsController extends GetxController {

  RxList<Device> myRobotList = RxList<Device>();

  @override
  void onInit() {
    super.onInit();
    getMineRobots();
  }


  @override
  void onReady() {
    super.onReady();
   /* eventBus.on<ProductContentEvent>().listen((event) {
      ProductContentEvent productContentEvent =  event as ProductContentEvent;

      if (productContentEvent.code == ROBOT_LIST_HAVE_CHANGED) {
         //检查robot是否再侧滑编辑,如果在侧滑编辑，则不更新列表.
         bool isEditing = false;
         if(myRobotList.value.isNotEmpty) {
           myRobotList.value.forEach((item) {
              if(!item.slideController!.dismissed) {
                isEditing = true;
              }
           });
         }

         if (!isEditing) {
           logger.info('更新我得机器人列表数据.');
           myRobotList.value = CommonUtil.getMyRobotLists();
           myRobotList.value.forEach((item) {
             item.slideController = SlideController(
               usePostActionController: true,
             );
           });
         }

      }

    });*/

  }


  @override
  void onClose() {
    super.onClose();
  }


  void getMineRobots() {
    myRobotList.value = CommonUtil.getMyRobotLists();
    myRobotList.value.forEach((item) {
      item.slideController = SlideController(
        usePostActionController: true,
      );
    });
  }

  unBindRobote(String robotUserId) {
    NetWorkManager.getInstance().requestBussiness(NetWorkManager.BIND_CONFIG_ROBOTE_UNBIND, {
      'robotUserId': robotUserId
    }, NetWorkManager.methodPOST, DataCallBack(
        businessCallBack: (v) {
          MqttCommManager().unsubscribeMsg(robotUserId);

          StorageManager.getStorage().remove(AppConst.BLT_DEVICE_SN_CURRENTLY);

          IndexViewController indexViewController = GetInstance().find<IndexViewController>();
          if (indexViewController != null) {
            indexViewController.retrieveRobots();
          }

          //清空解绑得机器人.
          Device unbindDevice = Device();
          myRobotList.forEach((item) {
             if (item.robotUserId == robotUserId) {
               unbindDevice = item;
             }
          });

          if (unbindDevice.robotUserId != null)
          myRobotList.remove(unbindDevice);

          //Get.back();

          Tips.showToast('解绑成功');

          // print('bingd -----解绑成功----------> $v');
        }
    ),showLoading: true);

  }
}
